$(function () {
    var appId = "wxe1788ab35bdbf471";
    var redirecturl = "http://wap.soolife.cn/view/account/login_wechat.html";
    var wechatloginUri = "http://wap.soolife.cn/server/wechatlogin.php";
    // 1. 请求CODE
    if (window.location.search.indexOf('code') == -1) {
        // 1 获取access_token
        //https://open.weixin.qq.com/connect/qrconnect?appid=wxe1788ab35bdbf471&redirect_uri=http://wap.soolife.cn/view/account/login_wechat.html&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect
        var path = 'https://open.weixin.qq.com/connect/qrconnect?';
        var queryParams = ['appid=' + appId,
            'redirect_uri=' + redirecturl,
            'response_type=code&scope=snsapi_login&state=STATE#wechat_redirect'];

        var query = queryParams.join('&');
        var url = path + query;
        window.location.href = url;
    } else {
        var code = getUrlParam("code");
        //向服务端传输code及openid参数
        $.ajax({
            type: "GET", url: wechatloginUri + "?code=" + code
        }).done(function (res) {
            var response = JSON.parse(res);
            if (!response.openid) {
                $.alert("登录授权失败，请稍后重试");
                return;
            }
            var userinfo = {typeid: 1, openid: response.openid, name: response.nickname};
            // 验证用户信息
            var _verfiyAccountModel = new verfiyAccountModel();
            _verfiyAccountModel.verifyAccount(userinfo);
        });

    }
});